#include <string>
#include <vector>

using namespace std;

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        int l = strs[0].length();
        int n = strs.size();

        int i = 0;

        for (; i < l; i++) {
            char& c = strs[0][i];

            for (int j = 1; j < n; j++) {
                // 遍历其他字符串，匹配字符是否相同
                if (strs[j].size() <= i || strs[j][i] != c) {
                    return strs[0].substr(0, i);
                }
            }
        }

        return strs[0].substr(0, i); 
    }
};